package com.ds.controller;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: SpringBoot
 * @description:
 * @author: Dawson.Zhang
 * @create: 2020-04-25
 **/
@ControllerAdvice
public class MyExceptionHandler {
//    定制异常json数据
//    @ResponseBody
//    @ExceptionHandler(MyException.class)
//    public Map<String,Object> handleException(Exception e){
//        Map<String,Object> map = new HashMap<>();
//        map.put("code","user.notexist");
//        map.put("message",e.getMessage());
//        return map;
//    }
    //返回异常视图，并且返回异常数据
    @ExceptionHandler()
    public String myException(Exception e, HttpServletRequest request){
        Map<String,Object> map = new HashMap<>();
        request.setAttribute("javax.servlet.error.status_code",500);
        map.put("code","usernotexist");
        map.put("msg","用户出错啦");
        request.setAttribute("ext",map);
        return "forward:/error";
    }
}
